home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-desktop-9.10-i386-PL.iso / casper / filesystem.squashfs / usr / share / pyshared / aptdaemon / policykit1.py < prev    next >
Text File  |  2009-10-14  |  5KB  |  137 lines

  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. """Provides access to PolicyKit privilege mangement using gdefer Deferreds."""
  4. # Copyright (C) 2008-2009 Sebastian Heinlein <devel@glatzor.de>
  5. #
  6. # This program is free software; you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation; either version 2 of the License, or
  9. # any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License along
  17. # with this program; if not, write to the Free Software Foundation, Inc.,
  18. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19.  
  20. __author__  = "Sebastian Heinlein <devel@glatzor.de>"
  21.  
  22. import os
  23.  
  24. import dbus
  25.  
  26. from defer import Deferred, defer
  27.  
  28. PK_ACTION_REMOVE_PACKAGES = "org.debian.apt.remove-packages"
  29. PK_ACTION_INSTALL_PACKAGES = "org.debian.apt.install-packages"
  30. PK_ACTION_INSTALL_FILE = "org.debian.apt.install-file"
  31. PK_ACTION_UPGRADE_PACKAGES = "org.debian.apt.upgrade-packages"
  32. PK_ACTION_UPDATE_CACHE = "org.debian.apt.update-cache"
  33. PK_ACTION_UPGRADE_SYSTEM = "org.debian.apt.upgrade-system"
  34. PK_ACTION_CANCEL_FOREIGN = "org.debian.apt.cancel-foreign"
  35. PK_ACTION_ADD_VENDOR_KEY = "org.debian.apt.add-vendor-key"
  36. PK_ACTION_GET_TRUSTED_VENDOR_KEYS = "org.debian.apt.get-trusted-vendor-keys"
  37. PK_ACTION_REMOVE_VENDOR_KEY = "org.debian.apt.remove-vendor-key"
  38. PK_ACTION_CHANGE_REPOSITORY = "org.debian.apt.change-repository"
  39.  
  40. CHECK_AUTH_NONE = 0
  41. CHECK_AUTH_ALLOW_USER_INTERACTION = 1
  42.  
  43.  
  44. class NotAuthorizedError(dbus.DBusException):
  45.  
  46.     def __init__(self, subject, action_id):
  47.         message = "%s is not authorized: %s" % (subject, action_id)
  48.         dbus.DBusException.__init__(self, message,
  49.                                     name="org.freedesktop.PolicyKit.Error."
  50.                                          "NotAuthorized")
  51.         self.action_id = action_id
  52.         self.subject = subject
  53.  
  54. def check_authorization_by_name(dbus_name, action_id, timeout=300, bus=None):
  55.     """Check if the given sender is authorized for the specified action.
  56.  
  57.     If the sender is not authorized raise NotAuthorizedError.
  58.  
  59.     Keyword arguments:
  60.     dbus_name -- D-Bus name of the subject
  61.     action_id -- the PolicyKit policy name of the action
  62.     timeout -- time in seconds for the user to authenticate
  63.     bus -- the D-Bus connection (defaults to the system bus)
  64.     """
  65.     subject = ("system-bus-name", {"name": dbus_name})
  66.     return _check_authorization(subject, action_id, timeout, bus)
  67.  
  68. def check_authorization_by_pid(pid, action_id, timeout=300, bus=None):
  69.     """Check if the given process is authorized for the specified action.
  70.  
  71.     If the sender is not authorized raise NotAuthorizedError.
  72.  
  73.     Keyword arguments:
  74.     pid -- id of the process
  75.     action_id -- the PolicyKit policy name of the action
  76.     timeout -- time in seconds for the user to authenticate
  77.     bus -- the D-Bus connection (defaults to the system bus)
  78.     """
  79.     subject = ("unix-process", {"pid": pid})
  80.     return _check_authorization(subject, action_id, timeout, bus)
  81.  
  82. def _check_authorization(subject, action_id, timeout, bus):
  83.     def policykit_done((authorized, challenged, auth_details)):
  84.         if authorized:
  85.             deferred.callback(auth_details)
  86.         else:
  87.             deferred.errback(NotAuthorizedError(subject, action_id))
  88.     if not bus:
  89.         bus = dbus.SystemBus()
  90.     deferred = Deferred()
  91.     pk = bus.get_object("org.freedesktop.PolicyKit1",
  92.                         "/org/freedesktop/PolicyKit1/Authority")
  93.     details = {}
  94.     pk.CheckAuthorization(subject, action_id, details,
  95.                           CHECK_AUTH_ALLOW_USER_INTERACTION, "",
  96.                           dbus_interface="org.freedesktop.PolicyKit1.Authority",
  97.                           timeout=timeout,
  98.                           reply_handler=policykit_done,
  99.                           error_handler=deferred.errback)
  100.     return deferred
  101.  
  102. def get_pid_from_dbus_name(dbus_name, bus=None):
  103.     """Return a deferred that gets the id of process owning the given
  104.     system D-Bus name.
  105.     """
  106.     if not bus:
  107.         bus = dbus.SystemBus()
  108.     deferred = Deferred()
  109.     bus_obj = bus.get_object("org.freedesktop.DBus",
  110.                              "/org/freedesktop/DBus/Bus")
  111.     bus_obj.GetConnectionUnixProcessID(dbus_name,
  112.                                        dbus_interface="org.freedesktop.DBus",
  113.                                        reply_handler=deferred.callback,
  114.                                        error_handler=deferred.errback)
  115.     return deferred
  116.  
  117. def get_uid_from_dbus_name(dbus_name, bus=None):
  118.     """Return a deferred that gets the uid of the user owning the given
  119.     system D-Bus name.
  120.     """
  121.     if not bus:
  122.         bus = dbus.SystemBus()
  123.     deferred = get_pid_from_dbus_name(dbus_name)
  124.     deferred.add_callback(_get_uid_from_pid)
  125.     return deferred
  126.  
  127. def _get_uid_from_pid(pid):
  128.     """Return the uid of the process."""
  129.     proc = open("/proc/%s/status" % pid)
  130.     values = [v for v in proc.readlines() if v.startswith("Uid:")]
  131.     proc.close()
  132.     uid = int(values[0].split()[1])
  133.     return uid
  134.  
  135.  
  136. # vim:ts=4:sw=4:et
  137.